package com.dataseek.iot.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataseek.iot.device.entity.MqttLog;
import com.dataseek.iot.device.mapper.MqttLogMapper;
import com.dataseek.iot.device.service.MqttLogService;
import com.dataseek.iot.device.vo.MqttLogVo;
import com.dataseek.iot.toolkit.tenant.config.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class MqttLogServiceImpl extends ServiceImpl<MqttLogMapper, MqttLog> implements MqttLogService {


    @Autowired
    private MqttLogMapper mqttLogMapper;

    @Override
    public MqttLogVo queryAction(String topic, String tenant_code, Integer num, Integer size) {

        MqttLogVo logVo = new MqttLogVo();
        QueryWrapper<MqttLog> qw = new QueryWrapper<>();
        qw.like("topic", topic);
        IPage<MqttLog> page = new Page<>(num, size);
        List<MqttLog> mapList = mqttLogMapper.selectPage(page, qw).getRecords();
        logVo.setPage(num);
        logVo.setSize(size);
        logVo.setTotal(page.getTotal());
        logVo.setList(mapList);
        return logVo;
    }

    @Override
    public void insertAction(MqttLog mqttLog) {
        String tenantCode = DynamicDataSourceContextHolder.getDataSourceKey();
        mqttLog.setTenantCode(tenantCode);
        this.saveOrUpdate(mqttLog);
    }

}
